package com.java1412.controller;

import com.java1412.dao.UserRepository;
import com.java1412.entity.Users;
import com.java1412.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author java1412
 * @create 2021-07-02 14:56
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RedisService redisService;

    @GetMapping("/{id}")
    public Users findUserById(@PathVariable("id") Integer id) {
        Users users = (Users) redisService.getObj("users" + id);
        if (users == null) {
            users = userRepository.findById(id).get();
            redisService.setObj("users" + id, users, 1000 * 60 * 2);
            return users;
        }
        return users;
    }
}
